<?php

namespace services\models\discount;

use common\models\discount\DiscountMessage;
use common\models\OrderDiscount;
use Yii;

/**
 * This is the model class for table "full_cut".
 *
 * @property integer $full_cut_id
 * @property string $title
 * @property integer $c_obj
 * @property integer $c_type
 * @property string $begin_time
 * @property string $end_time
 * @property integer $c_status
 * @property string $create_time
 */
class FullCut extends \common\models\discount\FullCut
{
    public static $redis_null = 'set is null';

    public function putDataToRedis()
    {
        /** @var static $data */
        $data = static::find()->filterWhere(['<', 'begin_time', date("Y-m-d H:i:s")])
            ->andFilterWhere(['>', 'end_time', date("Y-m-d H:i:s")])
            ->orFilterWhere(['>', 'begin_time', date("Y-m-d H:i:s")])
            ->andWhere(['c_status'=>self::$STATUS_ONLINE])
            ->orderBy(['begin_time' => 'asc'])
            ->one();
        if (empty($data)) {
            $this->redis->set($this->redis_key, static::$redis_null);
            return false;
        }
        $result_set = $data->getFullCutSet();
        $full_cut_set = [];
        foreach ($result_set as $item) {
            $full_cut_set[$item->total_amount] = $item->discount_amount;
        }
        ksort($full_cut_set);
        $str = [];
        foreach ($full_cut_set as $k => $v) {
            $str[] = "满{$k}减{$v}";
        }
        $data = \yii\helpers\BaseArrayHelper::toArray($data);
        $data['desc'] = implode('; ', $str);
        $data['sets'] = $full_cut_set;

        $this->redis->set($this->redis_key, serialize($data));
        return $data;

    }

    /**
     * 取redis中的促销数据 :
     * 如果过期, 设置redis数据为‘set is null’ ,返回false
     * 如果为空, 从数据库中取得数据存入redis , 当前时间满足时间范围,返回数据, 否则返回false
     * redis数据不为空情况下:满足时间限制返回数据, 如果时间范围不包含当前时间,返回false
     *
     * @return bool|mixed|FullCut|static
     */
    public function getDataFromRedis()
    {
        $data = $this->redis->get($this->redis_key);

        if ($data === static::$redis_null) {
            return false;
        }
        if (empty($data)) {
            $data = $this->putDataToRedis();
        } else {
            $data = unserialize($data);
        }

        if ($data['end_time'] <= date('Y-m-d H:i:s')) {
            $this->redis->set($this->redis_key, static::$redis_null);
            return false;
        }

        if ($data['begin_time'] >= date('Y-m-d H:i:s')) {
            return false;
        }
        return $data;


    }

    public function getFullCutSet()
    {

        return FullCutSet::find()->where(['full_cut_id' => $this->full_cut_id])->all();
    }

    /**
     * @param \services\models\Order $order
     * @return bool
     */
    public function getDiscountMoney(&$order)
    {
        $data = $this->getDataFromRedis();
        if ($data === false) return;

        $discount = null;
        foreach ($data['sets'] as $k => $v) {
            if ($order->goods_amount_sale >= $k) {
                $discount = $k;
            }
        }

        if ($discount !== null) {
            $dm = new OrderDiscount(['type' => static::shortClassName(), 'type_id' => $data['full_cut_id']]);
            $dm->discount_money = $data['sets'][$discount];
            $order->reduce_amount += $data['sets'][$discount];
            $dm->describe = '满' . $discount . '减' . $data['sets'][$discount];
            $order->addDiscountMsg($dm);

        }
    }


}
